(function () {
    app.controller("TsOrderSiteController", ['$scope', '$rootScope', 'tsOrderSiteService', function ($scope, $rootScope, tsOrderSiteService) {

        $scope.gridOptions = kendo.factory.getGridOptions(
            utils.url("./transportation/tsOrderSiteSearch.json"), "tsOrderId",
            [{
                field: "tsOrderNo",
                title: "运单号",
        /*        template: "<a href='\\#' ng-click='update(\"#:tsOrderId#\")'>#:tsOrderNo#</a>",*/
                width: 180
            }, {
                field: "projectName",
                title: "项目",
                width: 150,
                sortable: false
            }, {
                field: "clOrderNo",
                title: "客户单号",
                width: 150
            }, {
                field: "saleOrderNo",
                title: "发货单号",
                width: 150
            }, {
                field: "status",
                title: "状态",
                sortable: false,
                values: transportationOrderStatusStr,
                width: 100
            }, {
                field: "originCity",
                title: "始发城市",
                width: 140
            }, {
                field: "destCity",
                title: "目的城市",
                width: 140
            }, {
                field: "totalWeight",
                title: "总重量(㎏)",
                format: "{0:0.00}",
                width: 90
            }, {
                field: "totalVolume",
                title: "总体积(m³)",
                format: "{0:0.00}",
                width: 90
            }, {
                field: "totalPackageQty",
                title: "总件数",
                width: 90
            }, {
                field: "totalStandardPackage",
                title: "总标箱数",
                width: 90
            }, {
                field: "orderDate",
                title: "订单日期",
                width: 150
            }, {
                field: "orderType",
                title: "订单类型",
                sortable: false,
                values: orderCategoryStr,
                width: 100
            }], function () {
                return {query: $scope.tsOrderfilter};
            }
        );

        $scope.projectOptions = kendo.factory.getComboxOptions("/project/findByPermission", "projectId");

        $scope.tsOrderfilter = {};

        $rootScope.reloadTsGrid = function () {
            $scope.gridTsOrderSite.dataSource.read();
        };

        //分配
        $scope.distributionSite = function () {
            var ids = $scope.gridTsOrderSite.getSelectedId();
            if(ids.length < 1){
                kendo.factory.notifyError("请选择要分配站点的订单");
                return;
            }
            var selectData = $scope.gridTsOrderSite.getSelectedData();
            var projectId = selectData[0].projectId;
            for (var i = 0; i < selectData.length; i++) {
                var item = selectData[i];
                if(item.projectId != projectId){
                    kendo.factory.notifyError("请选择同一个项目的运单！");
                    return;
                }
            }
            $rootScope.$broadcast("distributionSite", ids);
        };
    }]);
})();

